﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YArchitech.Public
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYAutoDrawingT : HYExternalCommand
	{
		public override Result HYExecute(ref string TranName)
		{
			TranName = "自动成图";
			List<Element> elementByCategory = YJKElementFilter.GetElementByCategory(base.Doc, BuiltInCategory.OST_Views);
			List<Element> elementByCategory2 = YJKElementFilter.GetElementByCategory(base.Doc, typeof(FamilySymbol), BuiltInCategory.OST_TitleBlocks);
			Element element = null;
			Element element2 = null;
			Element element3 = null;
			Element element4 = null;
			Element element5 = null;
			foreach (Element element6 in elementByCategory2)
			{
				FamilySymbol familySymbol = (FamilySymbol)element6;
				if (familySymbol != null && familySymbol.Family.Name.Contains("建筑"))
				{
					element = familySymbol;
				}
				else if (familySymbol != null && familySymbol.Family.Name.Contains("结构"))
				{
					element2 = familySymbol;
				}
				else if (familySymbol != null && familySymbol.Family.Name.Contains("暖通"))
				{
					element3 = familySymbol;
				}
				else if (familySymbol != null && familySymbol.Family.Name.Contains("电气"))
				{
					element4 = familySymbol;
				}
				else if (familySymbol != null && familySymbol.Family.Name.Contains("给排水"))
				{
					element5 = familySymbol;
				}
				if (element != null && element3 != null && element2 != null && element4 != null && element5 != null)
				{
					break;
				}
			}
			if (element == null && elementByCategory2 != null && elementByCategory2.Count > 0)
			{
				element = elementByCategory2[0];
			}
			if (element3 == null && elementByCategory2 != null && elementByCategory2.Count > 0)
			{
				element3 = elementByCategory2[0];
			}
			if (element2 == null && elementByCategory2 != null && elementByCategory2.Count > 0)
			{
				element2 = elementByCategory2[0];
			}
			if (element4 == null && elementByCategory2 != null && elementByCategory2.Count > 0)
			{
				element4 = elementByCategory2[0];
			}
			if (element5 == null && elementByCategory2 != null && elementByCategory2.Count > 0)
			{
				element5 = elementByCategory2[0];
			}
			char[] trimChars = new char[]
			{
				'{',
				'}',
				'\\',
				':',
				'[',
				']',
				'|',
				';',
				'>',
				'<',
				'?',
				'`',
				'~'
			};
			foreach (Element element7 in elementByCategory)
			{
				Autodesk.Revit.DB.View view = (Autodesk.Revit.DB.View)element7;
				if (view != null && !view.IsTemplate && ((int)view.ViewType == 1 || (int)view.ViewType == 2 || (int)view.ViewType == 10 || (int)view.ViewType == 117 || (int)view.ViewType == 4 || (int)view.ViewType == 3))
				{
					Parameter parameter = view.GetParameter(BuiltInParameter.VIEWPORT_SHEET_NUMBER);
					if (parameter == null || !(parameter.AsString() != "") || parameter.AsString() == null)
					{
						ViewDiscipline discipline = view.Discipline;
						Element element8;
						if (discipline <= 8)
						{
							switch (discipline)
							{
							case 1:
								element8 = element;
								break;
							case 2:
								element8 = element2;
								break;
							case 3:
								goto IL_2C4;
							case 4:
								element8 = element3;
								break;
							default:
								if (discipline != 8)
								{
									goto IL_2C4;
								}
								element8 = element4;
								break;
							}
						}
						else if (discipline != 16)
						{
							if (discipline != 4095)
							{
								goto IL_2C4;
							}
							element8 = element;
						}
						else
						{
							element8 = element5;
						}
						IL_2C8:
						ViewSheet viewSheet = ViewSheet.Create(base.Doc, element8.Id);
						Viewport.Create(base.Doc, viewSheet.Id, view.Id, XYZ.Zero);
						viewSheet.Name = view.Name.Trim(trimChars);
						continue;
						IL_2C4:
						element8 = element3;
						goto IL_2C8;
					}
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
